﻿using System;
using System.Collections.Generic;
 
namespace Kernelx.Contexts.Core
{

    /// <summary>
    /// 提供初步封装的<see cref="IRequestParameterCollection"/>的实现基类
    /// </summary>
    public abstract class RequestParameterCollectionBase : IRequestParameterCollection
    {

        private Lazy<IDictionary<string, object>> _kvDict;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="getDict"></param>
        public RequestParameterCollectionBase(Func<IDictionary<string, object>> getDict) => _kvDict = new Lazy<IDictionary<string, object>>(getDict);


        /// <summary>
        /// 
        /// </summary>
        public IEnumerable<string> Keys => _kvDict.Value.Keys;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object this[string key] => _kvDict.Value.ContainsKey(key)
        || _kvDict.Value.ContainsKey(key = $"{key.Substring(0, 1).ToLower()}{key.Substring(1)}")
        || _kvDict.Value.ContainsKey(key = $"{key.Substring(0, 1).ToUpper()}{key.Substring(1)}")
        ? _kvDict.Value[key] : null;

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            _kvDict.Disposable();
            _kvDict = null;
        }
    }
}
